package com.ruoyi.home.jinrong.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.home.jinrong.entity.request.CwDealPerDaySearchPage;
import com.ruoyi.home.jinrong.entity.response.*;
import com.ruoyi.home.jinrong.service.CwDealPerDayService;
import com.ruoyi.home.jinrong.service.IZbfExportTaskHeaderService;
import com.ruoyi.home.jinrong.service.IZbfJobTypeService;
import com.ruoyi.home.utils.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author 郭豪
 * @date 2021/8/1 19:19
 * @description:
 */
@RestController
@RequestMapping("jinrong")
public class CwDealPerDayController{
    @Autowired
    private CwDealPerDayService cwDealPerDayService;

    @Autowired
    private IZbfExportTaskHeaderService jobHeaderService;

    @Autowired
    private IZbfJobTypeService jobTypeService;

    /**
     *  营业报表
     *  查询列表信息
     *  收入/支出 金额总计查询
     * @param cwDealPerDaySearchPage
     * @return
     */
    @RequestMapping("/searchCwDealPerDayList")
    public AjaxResult searchCwDealPerDayList( @RequestBody CwDealPerDaySearchPage cwDealPerDaySearchPage){
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(cwDealPerDaySearchPage.getPageNum(),cwDealPerDaySearchPage.getPageSize());
        //查询列表信息
        List<CwDealPerDayDto> cwDealPerDayDtos = cwDealPerDayService.searchCwDealPerDayPage(cwDealPerDaySearchPage);
        //收入/支出 金额总计查询
        CwDealPerDayTotalDto searchCwDealPerDaySum = cwDealPerDayService.searchCwDealPerDaySum(cwDealPerDaySearchPage);
        PageInfo<CwDealPerDayDto> pageInfo = new PageInfo<>(cwDealPerDayDtos);
        map.put("list" , pageInfo);
        map.put("sum" , searchCwDealPerDaySum);
        return AjaxResult.success(map);
    }


    /**
     * 添加导出excel任务
     * @param map
     */
    @RequestMapping("/getCwDealPerDayExcel")
    public String getCwDealPerDayExcel(@RequestBody Map<String , String > map){
        //查询参数
        CwDealPerDaySearchPage cwDate = new CwDealPerDaySearchPage();
        cwDate.setOperationType(Integer.valueOf(map.get("operationType")));//类型  收入、支出
        cwDate.setStartDate(map.get("startDate"));//开始时间
        cwDate.setEndDate(map.get("endDate"));//结束时间
        //根据条件查询总条数
        Integer dataNum = cwDealPerDayService.getDataNum(cwDate);

        //添加任务头表数据
        ZbfExportTaskHeader header = new ZbfExportTaskHeader();

        //获取任务创建执行时间
        Date nowDate = new Date();
        //根据总条数判断分片个数
        Long taskTailNum = dataNum/100l;
        if (dataNum%100>0){
            taskTailNum++;
        }
        header.setId(MyUtil.getRandom()); //id
        header.setVersion("0"); //版本号
        header.setUserId(MyUtil.Id); //操作人id
        header.setUserEamil(MyUtil.Eamil); //邮件
        header.setTaskName("escel导出"); //任务名称
        header.setTaskStatus(0l); //任务状态
        header.setTaskTailNum(taskTailNum); //任务明细个数
        header.setTaskTailOkNum(0l); //任务明细完成个数
        header.setTaskSql(
                "{\"startDate\":\""+cwDate.getStartDate()+"\"," +
                "\"endDate\":\""+cwDate.getEndDate()+"\","+
                "\"operationType\":"+cwDate.getOperationType()+"}"); //任务sql
        header.setCreateTime(nowDate); //创建时间

        //向 任务头表 添加任务
        jobHeaderService.insertZbfExportTaskHeader(header);

        //描述
        String jobMark = "导出"+cwDate.getStartDate()+"至"+cwDate.getEndDate()+"的营业报表";
        //设置添加数据
        ZbfJobType zbfJobType = new ZbfJobType();
        zbfJobType.setId(new Random().nextLong()+1); //编码
        zbfJobType.setJobMark(jobMark); //描述
        zbfJobType.setJobClass("xxx-job"); //类
        zbfJobType.setCreateTime(nowDate); //创建时间

        //添加记录表
        jobTypeService.insertZbfJobType(zbfJobType);

        System.out.println("任务添加完毕");
        return "任务已添加";
    }

    /**
     * 查询【导出数据】列表
     */
    @PostMapping("/list")
    public Map<String , Object > list(@RequestBody CwDealPerDaySearchPage cwDate) {
        //查询数据
        List<CwDealPerDayDto> list = cwDealPerDayService.searchCwDealPerDayPage(cwDate);
        if (cwDate.getPageNum()!=null && cwDate.getPageSize()==null){
            //分页
            PageHelper.startPage(cwDate.getPageNum() , cwDate.getPageSize());
        }

        Map<String , Object > returnMap = new HashMap<>();
        returnMap.put("data" , list);
        returnMap.put("startDate",cwDate.getStartDate());
        returnMap.put("endDate",cwDate.getEndDate());
        System.out.println(JSON.toJSONString(returnMap));

        return returnMap;
    }

}
